-- This file is part of the Fantasy Grounds Open Foundation Ruleset project. 
-- For the latest information, see http://www.fantasygrounds.com/
--
-- Copyright 2008 SmiteWorks Ltd.
--
-- This file is provided under the Open Game License version 1.0a
-- Refer to the license.html file for the full license text
--
-- All producers of work derived from this material are advised to
-- familiarize themselves with the license, and to take special
-- care in providing the definition of Product Identity (as specified
-- by the OGL) in their products.
--
-- All material submitted to the Open Foundation Ruleset project must
-- contain this notice in a manner applicable to the source file type.


local rootnode = nil;
local datalock = false;

function onInit()
	-- Prepare the node holding group entries
	rootnode = window.getDatabaseNode();
	rootnode.onChildAdded = onGroupAdded;
	
	local groupnodes = rootnode.getChildren();
	for groupname, groupnode in pairs(groupnodes) do
		onGroupAdded(rootnode, groupnode);
	end

	registerMenuItem("Create Item", "insert", 5);
	
	applyFilter();
end

function onMenuSelection(selection)
	if selection == 5 then
		createSourcedWindow();
	end
end

function onGroupAdded(parent, groupnode)
	if datalock then
		return;
	end

	local entrynoderoot = groupnode.createChild("entries");
	entrynoderoot.onChildAdded = onEntryAdded;
	
	local entrynodes = entrynoderoot.getChildren();
	for entryname, entrynode in pairs(entrynodes) do
		onEntryAdded(entrynoderoot, entrynode);
	end
	
	groupnode.createChild("hidden", "number").onUpdate = updateGroupVisibility;
end

function updateGroupVisibility()
	applyFilter();
end

function onFilter(w)
	if w.getDatabaseNode().createChild("...hidden", "number").getValue() == 1 then
		return false;
	end
	
	return true;
end

function onEntryAdded(parent, child)
	if datalock then
		return;
	end

	for k,v in pairs(getWindows()) do
		if child == v.getDatabaseNode() then
			return;
		end
	end

	local w = createSourcedWindow(child);
end

function refreshSort()
	applySort();
end

function onSortCompare(w1, w2)
	if CombatManager.getTrackerSortCallback() then
		return CombatManager.getTrackerSortCallback()(w1, w2);
	end
	
	if w1.name.getValue() == "" then return true; end
	if w2.name.getValue() == "" then return false; end
	return string.upper(w1.name.getValue()) > string.upper(w2.name.getValue());
end

function onDrop(x, y, draginfo)
	if draginfo.isType("playercharacter") or draginfo.isType("shortcut") then
		local dropclass, dropdatasource = draginfo.getShortcutData();
		local trackerclass = CombatManager.getTrackerClass(dropclass);
		
		if trackerclass then
			datalock = true;
			window.setDataClassHint(trackerclass);
			local w = createNewTrackerWindow(trackerclass);
			datalock = false;

			if w.populate then
				w.populate(DB.findNode(dropdatasource), nil);
			end
		end
		
		return true;
	end
end

function getDefaultGroup()
	if not rootnode.getChild("default") then
		local defaultnode = rootnode.createChild("default");
		defaultnode.createChild("name", "string").setValue("Default");
		return defaultnode;
	end
	
	return rootnode.createChild("default")
end

function createSourcedWindow(datanode)
	datalock = true;
	local datasource = datanode or getDefaultGroup().createChild("entries").createChild();
	datalock = false;

	-- Determine class of window to create
	local linknode = datasource.getChild("link");
	local trackerclass = window.getDataClassHint();
	
	if linknode then
		trackerclass = CombatManager.getTrackerClass(linknode.getValue());
	end
	
	-- Create
	if trackerclass then
		return createWindowWithClass(trackerclass, datasource);
	end

	local defaulttype = CombatManager.getDefaultTrackerType();
	if defaulttype then
		return createWindowWithClass(defaulttype, datasource);
	else
		return createWindow(datasource);
	end
end

function createNewTrackerWindow(trackerclass)
	datalock = true;
	local datasource = getDefaultGroup().createChild("entries").createChild();
	datalock = false;

	return createWindowWithClass(trackerclass, datasource);
end
